clear all;
clc;



n = 128;

a=rand(n,n);

temp=a;

a(temp(:,:)<0.99) = 1; %Healthy percentage 99%
a(temp(:,:)>0.99 & temp(:,:)<0.994) = 2; %Healthy percentage .04%
a(temp(:,:)>0.994 & temp(:,:)<0.998) = 3; %Healthy percentage .04%
a(temp(:,:)>0.998) = 4; %Healthy percentage .02%
imagesc(a);
cmap = jet(4);                                          % assign colormap
 colormap(cmap)
 hold on
 L = line(ones(4), ones(4), 'LineWidth',2);               % generate line 
 set(L,{'color'},mat2cell(cmap,ones(1,4),3));            % set the colors according to cmap
 legend('H','I1','I2','D') 


 x = 2:n-1;
 y = 2:n-1;
 

rule = a;
%figure



 rule((rule(x,y-1)==2)|(rule(x,y+1)==2)|(rule(x-1,y)==2)|(rule(x+1,y)==2)|(rule(x-1,y-1)==2)...
          |(rule(x-1,y+1)==2)|(rule(3:n,y-1)==2)|(rule(x+1,y+1)==2))=2;
     
   rule((rule(x,y-1)==3)|(rule(x,y+1)==3)|(rule(x-1,y)==3)|(rule(x+1,y)==3)|(rule(x-1,y-1)==3)...
           |(rule(x-1,y+1)==3)|(rule(3:n,y-1)==3)|(rule(x+1,y+1)==3))=3;
 
       rule((rule(x,y-1)==4)|(rule(x,y+1)==4)|(rule(x-1,y)==4)|(rule(x+1,y)==4)|(rule(x-1,y-1)==4)...
           |(rule(x-1,y+1)==4)|(rule(3:n,y-1)==4)|(rule(x+1,y+1)==4))=4;

       imagesc(rule)
       cmap = jet(4);                                          % assign colormap
 colormap(cmap)
 hold on
 L = line(ones(4), ones(4), 'LineWidth',2);               % generate line 
 set(L,{'color'},mat2cell(cmap,ones(1,4),3));            % set the colors according to cmap
 legend('H','I1','I2','D')  % add as many legend entries as data
